using System.Collections;
using UnityEngine;

namespace VoxelGame.NPC
{
	public class Idle : NPCBehaviour
	{
		[SerializeField]
		private string[] _idleAnimations;

		[SerializeField]
		private AudioClip[] _sound;

		[SerializeField]
		[Range(0f, 100f)]
		private int _chanceToPlaySound;

		private void Update()
		{
			_npcControls.SimpleMove(Vector3.zero);
		}

		private void OnEnable()
		{
			float time = Random.Range(_minDuration, _maxDuration);
			PlayAnimation(RandomElement(_idleAnimations));
			StartCoroutine(IdleTime(time));
			if (_sound != null && _sound.Length >= 1 && Random.Range(0, 100) < _chanceToPlaySound)
			{
				PlaySound();
			}
		}

		private bool HasSound()
		{
			return _sound != null && _sound.Length > 1;
		}

		private void PlaySound()
		{
			_npcControls.PlaySound(RandomElement(_sound));
		}

		private IEnumerator IdleTime(float time)
		{
			yield return new WaitForSeconds(time);
			_npcControls.OnBehaviourFinished(this);
		}
	}
}
